package cn.kgc.controller;

import cn.kgc.pojo.HasPower;
import cn.kgc.pojo.UserRole;
import cn.kgc.service.Service_dyb;
import cn.kgc.util.Result;
import com.alibaba.fastjson.JSON;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.Duration;
import java.util.List;
import java.util.UUID;

@Controller
public class Controller_logon_dyb {
    @Resource
    private cn.kgc.dao.userDao_dyb userDao_dyb;

    @Resource
    private Service_dyb service_dyb;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @RequestMapping("/login")
    public String welcome() {
        return "login";
    }


//    @RequestMapping("/welcome")
//    public String welcome2() {
//        System.out.println("进来了");
//        return "welcome";
//    }


    @PostMapping("/dologin")
    @ResponseBody
    public Result dologin(@RequestParam String coding, @RequestParam String password, HttpServletRequest request) {

//        System.out.println("48行进来了");
//        System.out.println(user);
//        System.out.println(pwd);

        //mp-根据账号和密码得到整用户信息
        UserRole user = userDao_dyb.selUser(coding,password);//mysql
        System.out.println("student= " + user);

        //从请求头得到token
        String headerToken = request.getHeader("token");//本地

        System.out.println("拿到请求头的token================================"+headerToken);

        if (user != null) {
            //情况一：非第一次，已经有token，甭管是谁的token
            if (headerToken != null && !headerToken.equals("null")) {
                //抓取redis里面对象的当前用户信息，必须得做转换，否则会报错
                Object obj = redisTemplate.opsForValue().get(headerToken);
                if (obj != null) {
                    //把object强转成student对象
                    UserRole user1 = JSON.parseObject(JSON.toJSONString(obj), UserRole.class);
                    System.out.println("student1= " + user1);//redis
                    if (!user.getCoding().equals(user1.getCoding())) {
                        //生成token令牌
                        headerToken = UUID.randomUUID().toString();
                        //存到Redis数据库
                    }
                } else {
                    headerToken = UUID.randomUUID().toString();
                }
            } else {
                //情况二：第一次，没有token
                //生成token令牌
                headerToken = UUID.randomUUID().toString();
                //存到Redis数据库
            }

            redisTemplate.opsForValue().set(headerToken, user, Duration.ofMinutes(30L));//30分钟
            return new Result(headerToken, "登录成功", 100);
        }
        return new Result(null, "登录失败(用户名或密码有误)", 104);
    }


    /**
     * 从Redis获取登录信息
     *
     * @param request
     * @return
     */
    @GetMapping("view/getUserOfLogin")
    @ResponseBody
    public Result getUserOfLogin(HttpServletRequest request) {
        

        //从请求头得到token
        String token = request.getHeader("token");
        System.out.println("进来了，token=" + token);
        Object str = redisTemplate.opsForValue().get(token);
        System.out.println(str);
        if (str != null) {
            return new Result(str, "获取登录用户成功", 100);
        }
        return new Result(null, "获取登录用户失败", 104);
    }


    //查询全部学生信息
//    @GetMapping("/getStuList")
//    @ResponseBody
//    public Result getStuList() {
//        //查询所有学生信息
//        List<UserRole> list = userDao_dyb.selectList(null);
//
//        return new Result(list, "查询学生信息成功", 100);
//    }

    //查询全部学生信息
    @GetMapping("view/loginOut")
    @ResponseBody
    public Result loginOut(HttpServletRequest request) {
        String token = request.getHeader("token");
        //从redis删掉
        Boolean bo = redisTemplate.delete(token);

        return new Result(bo, "注销成功", 100);
    }

    @RequestMapping("/getPower")
    @ResponseBody
    public List getPower(@RequestParam Integer id){
        List<String> powerList = service_dyb.getPower(id);
        return powerList;
    }


    @RequestMapping("/getHasPower")
    public String getPower(Model model){
        List<HasPower> list = service_dyb.getHasPower();
        model.addAttribute("list",list);
        return "权限列表";
    }

    @RequestMapping("addindex")
    public String addindex(Integer i,Model model){
        System.out.println("============================================");
        model.addAttribute("index",i);
        return "权限列表";
    }

    @RequestMapping("upHasPower")
    @ResponseBody
    public boolean upHasPower(@RequestParam Integer id,@RequestParam String haspower){
        Boolean aBoolean = service_dyb.upHasPower(id, haspower);
        return aBoolean;
    }

    @RequestMapping("yanzheng")
    public String goYanZheng(){
        return "旧密码验证";
    }

    @RequestMapping("xiugaimima")
    public String xiugaimima(){
        return "修改密码";
    }

    @RequestMapping("upPass")
    @ResponseBody
    public Boolean upPass(@RequestParam Integer id,@RequestParam String password){
        Boolean bo = service_dyb.upPass(id, password);
        return bo;
    }
}
